package com.logic.modular.game.battle.process.battleStage.actionPhase.actionPhase2.item;

import com.logic.modular.game.battle.info.roundInfo.PvpBattleStatisticsInfoServiceImpl;
import com.logic.modular.game.battle.panel.Battle;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.logic.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.logic.modular.game.battle.process.domain.EffectTriggerInfo;
import com.logic.modular.game.battle.process.domain.Reason;
import com.logic.modular.game.battle.process.domain.SkillReleaseInfo;

import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.logic.modular.game.battle.process.skillEffectHandle.skillEffectSelect.SkillEffectTriggerHandle;
import com.comment.pojo.domain.logic.core.CoreSkillEffect;
import com.comment.pojo.domain.logic.core.CoreSkill;
import com.comment.pojo.domain.logic.core.CoreSkillStar;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 4
 * 释放技能(选择技能类型--->武将释放技能)
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReleaseSkillService {
    private final PlayerHeroAttributeServiceImpl attributeService;
    private final ReleaseSkillService1 skillService1;
    private final ReleaseSkillService2 skillService2;
    private final ReleaseSkillService3 skillService3;
    private final PlayerHeroAttributeServiceImpl playerHeroAttributeServiceImpl;
    private final PvpBattleStatisticsInfoServiceImpl pvpBattleStatisticsInfoService;

    /**
     * 武将释放技能通过 技能类型
     *
     * @return
     */
    public List<SkillReleaseInfo> heroReleaseSkills(
            @NotNull(message = "战斗编号不能为空") String battleUUID,
            @NotNull(message = "当前出手武将") PlayerHeroAttribute actWJ,
            @NotNull(message = "武将技能类型 1,主动 2,被动 3,指挥 4,追击") Integer skillType,
            @NotNull(message = "存活武将列表") List<PlayerHeroAttribute> actionList
    ) {
        Battle battle = Battle.get(battleUUID);
        Long battleId = battle.getBattleId();
        List<SkillReleaseInfo> list = new ArrayList<>();

        actWJ.event.put(skillType, "");

        Reason reason = attributeService.statusUseAndRefresh(actWJ, 25L);
        if (reason.isSuccess()) return null;

        actWJ.getSkillSlot().getSkillSlot().forEach((skillNo,skillSlot)->{
            if (skillSlot.getType().equals(skillType)) {
                List<PlayerHeroAttribute> actCamp = actionList.stream()
                        .filter(item -> item.getRankCamp().equals(actWJ.getRankCamp()))
                        .collect(Collectors.toList());

                List<PlayerHeroAttribute> defCamp = actionList.stream()
                        .filter(item -> !item.getRankCamp().equals(actWJ.getRankCamp()))
                        .collect(Collectors.toList());

                SkillReleaseInfo one = heroReleaseSkills(battleUUID,battleId, actWJ, skillNo, actCamp,defCamp);
                list.add(one);
            }
        });

        return list;
    }


    /**
     * 武将释放技能
     */
    private SkillReleaseInfo heroReleaseSkills(
            @NotNull(message = "战斗编号不能为空") String battleUUID,
            @NotNull(message = "战斗id不能为空") Long battleId,
            @NotNull(message = "当前出手武将") PlayerHeroAttribute actWJ,
            @NotNull(message = "技能卡槽编号") Integer skillSortNo,
            @NotNull(message = "行动方存活武将列表")List<PlayerHeroAttribute> actCamp,
            @NotNull(message = "防御方存活武将列表")List<PlayerHeroAttribute> defCamp

    ) {
        CoreSkill coreSkill =  actWJ.getSkillSlot().getSkillSlot().get(skillSortNo);
        CoreSkillStar coreSkillStar = actWJ.getSkillSlot().getSkillStarSlot().get(skillSortNo);
        boolean isRelease = skillService1.determineRelease(battleId, actWJ, coreSkill, coreSkillStar);

        // 基础部分
        Long coreSkillStarId = coreSkillStar.getId();
        Long coreSkillId = coreSkillStar.getSkillId();
        List<PvpSkillEffect> allOneItem = new ArrayList<>();

        SkillReleaseInfo skillReleaseInfo = new SkillReleaseInfo();
        skillReleaseInfo.setIsRelease(isRelease);
        skillReleaseInfo.setSourcePlayerHeroId(actWJ.getId());
        skillReleaseInfo.setSourceSkillId(coreSkillId);
        skillReleaseInfo.setSourceSkillStarId(coreSkillStarId);

        PlayerHeroAttribute playerHeroAttribute = playerHeroAttributeServiceImpl.get(battleUUID, actWJ.getId());

        if (isRelease) {
            List<CoreSkillEffect> list = skillService2.list(coreSkillStar);
            for (CoreSkillEffect coreSkillEffect : list) {
                log.info("==========================");
                if (coreSkillEffect.getId()==12) {
                    log.info("{}",coreSkillEffect);
                }
                List<PlayerHeroAttribute> targetList = skillService2.getTargetHeroes(
                        battleId, coreSkillEffect.getTargetRange(), defCamp, actWJ, actCamp);

                List<PvpSkillEffect> newSkillEffectList = skillService3.obtainSkillEffects(
                        battleUUID, coreSkill, coreSkillStar, actWJ, targetList,
                        coreSkillEffect
                );
                List<PvpSkillEffect> oneItem = SkillEffectTriggerHandle.applyNewSkillEffect(newSkillEffectList);

                allOneItem.addAll(oneItem);

            }
            List<EffectTriggerInfo> skillTriggerInfoList = allOneItem.stream().map(PvpSkillEffect::getSkillEffectTriggerInfo).collect(Collectors.toList());
            skillReleaseInfo.skillTriggerInfoList.addAll(skillTriggerInfoList);

            playerHeroAttribute.logSkillCount(coreSkillStarId);
            playerHeroAttributeServiceImpl.saveCache(playerHeroAttribute);
        }
        return skillReleaseInfo;
    }

}
